<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 1 ? '入库明细' : type === 2 ? '出库明细': '配送员库存明细'"
    :visible.sync="visible"
    width="400px"
    @closed="reset"
  >
    <lb-table
      ref="lbTable"
      border
      highlight-current-row
      :column="column"
      :data="tableData.data"
      :pagination="false"
    />
  </el-dialog>
</template>

<script>
  import { queryInStoreData, queryOutStoreData, queryOutOfStoreData } from '@/api'

  export default {
    name: 'InAndOutStoreDetail',
    data() {
      return {
        type: 1,
        visible: false,
        tableData: {
          data: []
        }
      }
    },
    computed: {
      column: function() {
        if (this.type === 1) {
          return [
            {
              prop: 'remark',
              label: '入库项'
            },
            {
              prop: 'gdCount',
              align: 'right',
              label: '入库数量',
              render: (h, { row }) => {
                if (row.pricingMode === 2) return <span>{row.gdCount || 0}</span>
                else return <span>{row.gdCount || 0}</span>
              }
            }
          ]
        } else if (this.type === 2) {
          return [
            {
              prop: 'remark',
              label: '出库项'
            },
            {
              prop: 'gdCount',
              align: 'right',
              label: '出库数量',
              render: (h, { row }) => {
                if (row.pricingMode === 2) return <span>{row.gdCount || 0}</span>
                else return <span>{row.gdCount || 0}</span>
              }
            }
          ]
        } else {
          return [
            {
              prop: 'rName',
              label: '配送员'
            },
            {
              prop: 'netWeight',
              align: 'right',
              label: '库存数量',
              render: (h, { row }) => {
                if (row.pricingMode === 2) return <el-link underline={false} onClick={() => this.storeDetail(row)}>{row.netWeight || 0}</el-link>
                else return <el-link underline={false} onClick={() => this.storeDetail(row)}>{row.gdCount || 0}</el-link>
              }
            }
          ]
        }
      }
    },
    methods: {
      open(temp, type) {
        console.log(temp)
        console.log(type)
        this.fetchData(temp, type)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          this.type = type
        })
      },
      async fetchData(temp, type) {
        const params = {
          storeId: temp.storeId,
          goodsId: temp.goodsId,
          specCode: temp.specCode,
          gdStatus: temp.gdStatus
        }
        let res
        if (type === 1) {
          res = await queryInStoreData(params)
          this.tableData.data = res.data.stockInList
        } else if (type === 2) {
          res = await queryOutStoreData(params)
          console.log(res)
          this.tableData.data = res.data.stockOutList
        } else if (type === 3) {
          res = await queryOutOfStoreData({
            ...params,
            gtId: temp.gtId,
            goodsName: temp.goodsName
          })
          this.tableData.data = res.data.storeDetailList
        }
      },
      storeDetail(row) {
        this.$emit('storeDetail', row)
      },
      reset() {
        this.type = 1
      }
    }
  }
</script>

<style scoped>

</style>
